
#include "../Application/Application.h"
#include "../Layout/HorizontalLayout.h"
#include "../Layout/VerticalLayout.h"
#include "Button.h" 

namespace GUI
{
	namespace Win32
	{

		Button::Button(const std::string & _strLabel) : m_strLabel(_strLabel), m_iWidth(-1), m_iHeight(-1)
		{
			m_iId = Application::GetId();
			Application::RegisterWidget(this);
		}

		Button::Button(const std::string & _strLabel, int _iWidth, int _iHeight) : m_strLabel(_strLabel), m_iWidth(_iWidth), m_iHeight(_iHeight)
		{
			m_iId = Application::GetId();
			Application::RegisterWidget(this);
		}

		Button::~Button()
		{
			Application::UnregisterWidget(this);
		}

		void Button::Create(HWND _hWindow, HINSTANCE _hInstance)
		{
			m_hButton = CreateWindowEx(NULL, "BUTTON", GetLabel().c_str(), WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, GetX(), GetY(), GetWidth(), GetHeight(), _hWindow, (HMENU)m_iId, _hInstance, NULL);
		}

		Core::GUI::WidgetType Button::GetType() const
		{
			return Core::GUI::WidgetType::Button;
		}

		int Button::GetId() const
		{
			return m_iId;
		}

		void Button::Accept(Core::GUI::IVerticalLayout * _pVerticalLayout)
		{
			((VerticalLayout*)_pVerticalLayout)->Visit(this);
		}

		void Button::Accept(Core::GUI::IHorizontalLayout * _pHorizontalLayout)
		{
			((HorizontalLayout*)_pHorizontalLayout)->Visit(this);
		}
		
		const std::string & Button::GetLabel() const
		{
			return m_strLabel;
		}

		int Button::GetWidth() const
		{
			return m_iWidth;
		}

		int Button::GetHeight() const
		{
			return m_iHeight;
		}

		int Button::GetX() const
		{
			return m_iX;
		}

		int Button::GetY() const
		{
			return m_iY;
		}

		void Button::Resize(int _iWidth, int _iHeight)
		{
			m_iWidth = _iWidth;
			m_iHeight = _iHeight;
			MoveWindow(m_hButton, m_iX, m_iY, _iWidth, _iHeight, true);
		}

		void Button::Move(int _x, int _y)
		{
			m_iX = _x;
			m_iY = _y;
			MoveWindow(m_hButton, _x, _y, m_iWidth, m_iHeight, true);
		}

		void Button::OnClick(VoidFunctionVoid _function)
		{
			m_fctOnClick = _function;
		}

		void Button::Click()
		{
			if (m_fctOnClick)
			{
				m_fctOnClick();
			}
		}

	}
}